package com.beta.cust.bind.utils;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;


public class PinyinUtil {

    private PinyinUtil() {
        throw new IllegalStateException("Utility class");
    }

    private static final Logger LOGGER = LoggerFactory.getLogger(PinyinUtil.class);

    private static HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();

    static {
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    }

    /**
     * 将中文转化为拼音
     */
    public static String getPinYinString(String string) {
        if (StringUtils.isBlank(string)) {
            return "";
        }
        try {
            StringBuilder builder = new StringBuilder();
            int index = 0;

            /*MultiPinYinEnum multiPinYinEnum = MultiPinYinEnum.getMultiPinYinEnum(string.charAt(0));
            if (multiPinYinEnum != null) {
                builder.append(multiPinYinEnum.getPinyin());
                index = 1;
            }*/

            for (int i = index; i < string.length(); i++) {
                char word = string.charAt(i);
                String[] strings = PinyinHelper.toHanyuPinyinStringArray(word, format);
                if (null != strings && strings.length > 0) {
                    builder.append(strings[0]);
                } else {
                    builder.append(word);
                }
            }
            return builder.toString();
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            LOGGER.error("HanyuPinyin getPinYinString error, str:{}", string, e);
        }

        return "";
    }

    /**
     * 获取中文的首字符
     */
    public static String getPinYinFirstChar(String string) {
        if (StringUtils.isBlank(string)) {
            return "";
        }

        try {
            StringBuilder builder = new StringBuilder();
            int index = 0;

            /*MultiPinYinEnum multiPinYinEnum = MultiPinYinEnum.getMultiPinYinEnum(string.charAt(0));
            if (multiPinYinEnum != null) {
                builder.append(multiPinYinEnum.getInitial());
                index = 1;
            }*/

            for (int i = index; i < string.length(); i++) {
                char word = string.charAt(i);
                String[] strings = PinyinHelper.toHanyuPinyinStringArray(word, format);
                if (null != strings && strings.length > 0) {
                    builder.append(strings[0].charAt(0));
                } else {
                    builder.append(word);
                }
            }
            return builder.toString().toUpperCase();
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            LOGGER.error("HanyuPinyin Format error, str:{}", string, e);
        }
        return "";
    }

    /**
     * 获取拼音的首字符
     */
    public static String getFirstChar(String pinStr) {
        if (StringUtils.isBlank(pinStr)) {
            return "";
        }
        try {
            return pinStr.toUpperCase().substring(0, 1);
        } catch (Exception e) {
            LOGGER.error("HanyuPinyin getPinYinFirstChar error, str:{}", pinStr, e);
        }
        return "";
    }

    /**
     * 判断是否为汉字
     *
     * @param chars
     * @return
     */
    public static boolean isChineseChar(String chars) {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(chars);
        if (m.find()) {
            return true;
        }
        return false;
    }

}
